﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Media;
using System.Drawing.Drawing2D; 

namespace selectpicture
{
    public partial class Bibo : Form
    {
        public Bibo()
        {
            InitializeComponent();
        }
        private bool flag=false;
        FileInfo flInfo;
        private bool btnflag = true;

        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarinset);
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int Right;
            public int left;
            public int Top;
            public int Bottom;
        }

       /// <summary>
       /// MCI 多媒体数据接口初始化
       /// </summary>
        public static uint SND_ASYNC = 0x0001;  // play asynchronously 
        public static uint SND_FILENAME = 0x00020000; // name is file name
        [DllImport("winmm.dll")]
        public static extern uint mciSendString(string lpstrCommand, 
        string lpstrReturnString, uint uReturnLength, uint hWndCallback);
      

        /// <summary>
        /// 开始按钮，点击自动隐藏，开始播放抽奖音乐
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            timer1.Enabled = true;
            flag = true;
            btnflag = false;
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            this.Click += new System.EventHandler(this.Form1_Click);
            mci("bv.mp3");
            
          }

        private void mci(string e)
        {
            mciSendString(@"close temp_alias", null, 0, 0);
            mciSendString(@"open " + "\""+ e + "\"" + " alias temp_alias", null, 0, 0);
            mciSendString("play temp_alias  repeat", null, 0, 0);
        }

        /// <summary>
        /// 读取image文件夹文件
        /// </summary>
        private void displaypicture()
        {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Application.StartupPath + "\\image\\");
            FileInfo[] ff = di.GetFiles("*.jpg");
            Random random = new Random();//随机
            int a = random.Next(0, ff.Length);
            if (ff.Length == 0)
            //if (di.GetFiles().Length + di.GetDirectories().Length == 0)
            {
                timer1.Enabled = false;
                //winner文件夹为空 提示所有人中奖 并播放音乐
                mci("allwin.mp3");
                MessageBox.Show("所有人都已经中奖");

            }
            else {
                //把文件写入memory，方便删除文件
                flInfo = ff[a];
                FileStream fs = new FileStream(Application.StartupPath + "\\image\\" + flInfo.ToString(), FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader br = new BinaryReader(fs);
                MemoryStream ms = new MemoryStream(br.ReadBytes((int)fs.Length));
                pictureBox1.Image = Image.FromStream(ms);
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                fs.Close();
            }
           
        }
        /// <summary>
        /// 窗体载入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            this.BackgroundImage = null;
            MARGINS margins = new MARGINS();
            margins.left = -1;
            margins.Right = -1;
            margins.Top = -1;
            margins.Bottom = -1;
            IntPtr hwnd = Handle;
            int result = DwmExtendFrameIntoClientArea(hwnd, ref margins);

            this.BackColor = Color.Black;
            int w = this.ClientRectangle.Width-button1.Width;
            int h = this.ClientRectangle.Height-button1.Height;
            button1.Location = new Point(w/2,h/2);
            if(File.Exists(Application.StartupPath + "\\bd.jpg")){
                Image ig = Image.FromFile(Application.StartupPath + "\\bd.jpg", true);

                this.BackgroundImage = ig;
            }
           
        }
        /// <summary>
        /// 定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            displaypicture();
        }
        /// <summary>
        /// 定时器开关
        /// </summary>
        private void judge()
        {
            if (flag==true)
            {
                timer1.Enabled = false;
                flag = false;
                //中奖后音乐
                //mci("win.mp3");
                mciSendString(@"close temp_alias", null, 0, 0);
                mciSendString(@"open " + "win.mp3" + " alias temp_alias", null, 0, 0);
                mciSendString("play temp_alias", null, 0, 0);
                
                //中奖图片移动到winner文件夹
                if (flInfo == null)
                {
                    mci("allwin.mp3");
                    MessageBox.Show("所有人都已经中奖");
                }
                else { 

                if (File.Exists(Application.StartupPath + "\\winner\\" + flInfo.ToString()))
                {
                    new FileInfo(Application.StartupPath + "\\winner\\" + flInfo.ToString()).Delete();         
                }

                try
                {
                //flInfo.CopyTo(Application.StartupPath + "\\winner\\" + flInfo.ToString(), true);
                flInfo.MoveTo(Application.StartupPath + "\\winner\\" + flInfo.ToString());
                //File.Delete(Application.StartupPath + "\\image\\" + flInfo.ToString());//移动后删除
                }
                catch (Exception e)
                {
                    mci("allwin.mp3");
                    MessageBox.Show("所有人都已经中奖");
                }
                                 
                
                }
                
            }
            else
            {
                //回归背景音乐
                timer1.Enabled = true;
                flag = true;
                mci("bv.mp3");
            }
 
        }
        //胜利人员列表
        private void winlist()
        {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Application.StartupPath + "\\winner\\");
            FileInfo[] ff = di.GetFiles("*.jpg");
            if (di.GetFiles().Length + di.GetDirectories().Length == 0)
            {
                MessageBox.Show("还没有人中奖呢，亲！");
            }
            else
            {

                winners w = new winners(ff);
                w.Show();

            }

        }
        //单击抽奖
       private void pictureBox1_Click(object sender, EventArgs e)
        {

           button1.Visible = false;
           
               judge();
  

        }
      
        private void Form1_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            judge();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                
                case Keys.Enter://回车抽奖
                    if (btnflag == false)
                    {
                        judge();
                        button1.Visible = false;
                        
                    }
                     break;
                case Keys.A://A 调用胜利人员列表
                    winlist();
                    break;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
